//
//  DateCollectionViewCell.m
//  TableTest
//
//  Created by 众拳科技 on 2017/11/14.
//  Copyright © 2017年 众拳科技. All rights reserved.
//

#import "DateCollectionViewCell.h"

@implementation DateCollectionViewCell

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
                        
        /**
         周日
         */
        UILabel *sunLabel = [[UILabel alloc] init];
        sunLabel.textAlignment = NSTextAlignmentCenter;
        sunLabel.font = [UIFont systemFontOfSize:14];
        sunLabel.textColor = CSColorFromRGB(0xa5a5a5);
        sunLabel.text = @"周日";
        [self addSubview:sunLabel];
        self.sunLabel = sunLabel;
        
        UIButton *sunDetailLabel = [[UIButton alloc] init];
        sunDetailLabel.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [sunDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
        [sunDetailLabel setTitle:@""];
        [self addSubview:sunDetailLabel];
        self.sunDetailLabel = sunDetailLabel;
        
        /**
         周一
         */
        UILabel *monLabel = [[UILabel alloc] init];
        monLabel.textAlignment = NSTextAlignmentCenter;
        monLabel.font = [UIFont systemFontOfSize:14];
        monLabel.textColor = CSColorFromRGB(0xa5a5a5);
        monLabel.text = @"周一";
        [self addSubview:monLabel];
        self.monLabel = monLabel;
        
        UIButton *monDetailLabel = [[UIButton alloc] init];
        monDetailLabel.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [monDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
        [monDetailLabel setTitle:@""];
        [self addSubview:monDetailLabel];
        self.monDetailLabel = monDetailLabel;
        
        /**
         周二
         */
        UILabel *tueLabel = [[UILabel alloc] init];
        tueLabel.textAlignment = NSTextAlignmentCenter;
        tueLabel.font = [UIFont systemFontOfSize:14];
        tueLabel.textColor = CSColorFromRGB(0xa5a5a5);
        tueLabel.text = @"周二";
        [self addSubview:tueLabel];
        self.tueLabel = tueLabel;
        
        UIButton *tueDetailLabel = [[UIButton alloc] init];
        tueDetailLabel.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [tueDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
        [tueDetailLabel setTitle:@""];
        [self addSubview:tueDetailLabel];
        self.tueDetailLabel = tueDetailLabel;
        
        /**
         周三
         */
        UILabel *wedLabel = [[UILabel alloc] init];
        wedLabel.textAlignment = NSTextAlignmentCenter;
        wedLabel.font = [UIFont systemFontOfSize:14];
        wedLabel.textColor = CSColorFromRGB(0xa5a5a5);
        wedLabel.text = @"周三";
        [self addSubview:wedLabel];
        self.wedLabel = wedLabel;
        
        UIButton *wedDetailLabel = [[UIButton alloc] init];
        wedDetailLabel.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [wedDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
        [wedDetailLabel setTitle:@""];
        [self addSubview:wedDetailLabel];
        self.wedDetailLabel = wedDetailLabel;
        
        
        /**
         周四
         */
        UILabel *thuLabel = [[UILabel alloc] init];
        thuLabel.textAlignment = NSTextAlignmentCenter;
        thuLabel.font = [UIFont systemFontOfSize:14];
        thuLabel.textColor = CSColorFromRGB(0xa5a5a5);
        thuLabel.text = @"周四";
        [self addSubview:thuLabel];
        self.thuLabel = thuLabel;
        
        UIButton *thuDetailLabel = [[UIButton alloc] init];
        thuDetailLabel.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [thuDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
        [thuDetailLabel setTitle:@""];
        [self addSubview:thuDetailLabel];
        self.thuDetailLabel = thuDetailLabel;
        
        /**
         周五
         */
        UILabel *friLabel = [[UILabel alloc] init];
        friLabel.textAlignment = NSTextAlignmentCenter;
        friLabel.font = [UIFont systemFontOfSize:14];
        friLabel.textColor = CSColorFromRGB(0xa5a5a5);
        friLabel.text = @"周五";
        [self addSubview:friLabel];
        self.friLabel = friLabel;
        
        UIButton *friDetailLabel = [[UIButton alloc] init];
        friDetailLabel.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [friDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
        [friDetailLabel setTitle:@""];
        [self addSubview:friDetailLabel];
        self.friDetailLabel = friDetailLabel;
        
        /**
         周六
         */
        
        UILabel *satLabel = [[UILabel alloc] init];
        satLabel.textAlignment = NSTextAlignmentCenter;
        satLabel.font = [UIFont systemFontOfSize:14];
        satLabel.textColor = CSColorFromRGB(0xa5a5a5);
        satLabel.text = @"周六";
        [self addSubview:satLabel];
        self.satLabel = satLabel;
        
        UIButton *satDetailLabel = [[UIButton alloc] init];
        satDetailLabel.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [satDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
        [satDetailLabel setTitle:@""];
        [self addSubview:satDetailLabel];
        self.satDetailLabel = satDetailLabel;

        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor redColor];
        [self addSubview:lineView];
        self.lineView = lineView;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{

    //CGFloat width = (SCREEN_WIDTH - (SCREEN_WIDTH / 8)) / 7;
    
    
    /**
     周日
     */
    [self.sunLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(4);
        
        make.left.mas_equalTo(self);
        
//        make.width.mas_equalTo(width);
    }];
    
    [self.sunDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.sunLabel.mas_bottom).offset(12);
        
        make.centerX.mas_equalTo(self.sunLabel);
        
        make.width.height.mas_equalTo(30);
    }];
    
    /**
     周一
     */
    [self.monLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.width.mas_equalTo(self.sunLabel);
        
        make.left.mas_equalTo(self.sunLabel.mas_right);
    }];
    
    [self.monDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.monLabel.mas_bottom).offset(12);
        
        make.centerX.mas_equalTo(self.monLabel);
        
        make.width.height.mas_equalTo(30);
    }];
    
    /**
     周二
     */
    [self.tueLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.width.mas_equalTo(self.monLabel);
        
        make.left.mas_equalTo(self.monLabel.mas_right);
    }];
    
    [self.tueDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.tueLabel.mas_bottom).offset(12);
        
        make.centerX.mas_equalTo(self.tueLabel);
        
        make.width.height.mas_equalTo(30);
    }];
    
    /**
     周三
     */
    [self.wedLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.width.mas_equalTo(self.tueLabel);
        
        make.left.mas_equalTo(self.tueLabel.mas_right);
    }];
    
    [self.wedDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.wedLabel.mas_bottom).offset(12);
        
        make.centerX.mas_equalTo(self.wedLabel);
        
        make.width.height.mas_equalTo(30);
    }];
    
    
    /**
     周四
     */
    [self.thuLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.width.mas_equalTo(self.wedLabel);
        
        make.left.mas_equalTo(self.wedLabel.mas_right);
    }];
    
    [self.thuDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.thuLabel.mas_bottom).offset(12);
        
        make.centerX.mas_equalTo(self.thuLabel);
        
        make.width.height.mas_equalTo(30);
    }];
    
    /**
     周五
     */
    [self.friLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.width.mas_equalTo(self.thuLabel);
        
        make.left.mas_equalTo(self.thuLabel.mas_right);
    }];
    
    [self.friDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.friLabel.mas_bottom).offset(12);
        
        make.centerX.mas_equalTo(self.friLabel);
        
        make.width.height.mas_equalTo(30);
    }];
    
    /**
     周六
     */
    
    [self.satLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.width.mas_equalTo(self.friLabel);
        
        make.left.mas_equalTo(self.friLabel.mas_right);
        
        make.right.mas_equalTo(self);
    }];
    
    [self.satDetailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.satLabel.mas_bottom).offset(12);
        
        make.centerX.mas_equalTo(self.satLabel);
        
        make.width.height.mas_equalTo(30);
    }];
}

-(void)setFirstDayOfWeek:(NSDate *)firstDayOfWeek{
    
    _firstDayOfWeek = firstDayOfWeek;
    
    /**
     周日
     */
    NSInteger sunDay = [self getDayWithDate:firstDayOfWeek];
    [self.sunDetailLabel setTitle:[NSString stringWithFormat:@"%02ld",sunDay]];
    
    /**
     周一
     */
    NSDate *monDate = [self getDayWithDate:firstDayOfWeek andDayCount:1];
    NSInteger monDay = [self getDayWithDate:monDate];
    [self.monDetailLabel setTitle:[NSString stringWithFormat:@"%02ld",monDay]];
    
    /**
     周二
     */
    NSDate *tueDate = [self getDayWithDate:firstDayOfWeek andDayCount:2];
    NSInteger tueDay = [self getDayWithDate:tueDate];
    [self.tueDetailLabel setTitle:[NSString stringWithFormat:@"%02ld",tueDay]];
    
    /**
     周三
     */
    NSDate *wedDate = [self getDayWithDate:firstDayOfWeek andDayCount:3];
    NSInteger wedDay = [self getDayWithDate:wedDate];
    [self.wedDetailLabel setTitle:[NSString stringWithFormat:@"%02ld",wedDay]];
    
    /**
     周四
     */
    NSDate *thuDate = [self getDayWithDate:firstDayOfWeek andDayCount:4];
    NSInteger thuDay = [self getDayWithDate:thuDate];
    [self.thuDetailLabel setTitle:[NSString stringWithFormat:@"%02ld",thuDay]];
    
    /**
     周五
     */
    NSDate *friDate = [self getDayWithDate:firstDayOfWeek andDayCount:5];
    NSInteger friDay = [self getDayWithDate:friDate];
    [self.friDetailLabel setTitle:[NSString stringWithFormat:@"%02ld",friDay]];
    
    /**
     周六
     */
    //NSInteger satDay = [self getDayWithDate:firstDayOfWeek andDayCount:6];
    NSDate *satDate = [self getDayWithDate:firstDayOfWeek andDayCount:6];
    NSInteger satDay = [self getDayWithDate:satDate];
    [self.satDetailLabel setTitle:[NSString stringWithFormat:@"%02ld",satDay]];
    
    /**
     周日
     */
    if ([self dateIsToday:firstDayOfWeek]) {
        
        self.sunLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.sunDetailLabel.backgroundColor = CSBlackColor;
        [self.sunDetailLabel setTitleColor:CSWhiteColor];
    }
    else{
        
        self.sunLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.sunDetailLabel.backgroundColor = CSWhiteColor;
        [self.sunDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
    }
    
    /**
     周一
     */
    if ([self dateIsToday:monDate]) {
        
        self.monLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.monDetailLabel.backgroundColor = CSBlackColor;
        [self.monDetailLabel setTitleColor:CSWhiteColor];
    }
    else{
        
        self.monLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.monDetailLabel.backgroundColor = CSWhiteColor;
        [self.monDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
    }
    
    /**
     周二
     */
    if ([self dateIsToday:tueDate]) {
        
        self.tueLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.tueDetailLabel.backgroundColor = CSBlackColor;
        [self.tueDetailLabel setTitleColor:CSWhiteColor];
    }
    else{
        
        self.tueLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.tueDetailLabel.backgroundColor = CSWhiteColor;
        [self.tueDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
    }
    
    /**
     周三
     */
    if ([self dateIsToday:wedDate]) {
        
        self.wedLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.wedDetailLabel.backgroundColor = CSBlackColor;
        [self.wedDetailLabel setTitleColor:CSWhiteColor];
    }
    else{
        
        self.wedLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.wedDetailLabel.backgroundColor = CSWhiteColor;
        [self.wedDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
    }
    
    /**
     周四
     */
    if ([self dateIsToday:thuDate]) {
        
        self.thuLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.thuDetailLabel.backgroundColor = CSBlackColor;
        [self.thuDetailLabel setTitleColor:CSWhiteColor];
    }
    else{
        
        self.thuLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.thuDetailLabel.backgroundColor = CSWhiteColor;
        [self.thuDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
    }
    
    /**
     周五
     */
    if ([self dateIsToday:friDate]) {
        
        self.friLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.friDetailLabel.backgroundColor = CSBlackColor;
        [self.friDetailLabel setTitleColor:CSWhiteColor];
    }
    else{
        
        self.friLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.friDetailLabel.backgroundColor = CSWhiteColor;
        [self.friDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
    }
    
    /**
     周六
     */
    if ([self dateIsToday:satDate]) {
        
        self.satLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.satDetailLabel.backgroundColor = CSBlackColor;
        [self.satDetailLabel setTitleColor:CSWhiteColor];
    }
    else{
        
        self.satLabel.textColor = CSColorFromRGB(0xa5a5a5);
        self.satDetailLabel.backgroundColor = CSWhiteColor;
        [self.satDetailLabel setTitleColor:CSColorFromRGB(0x323232)];
    }
}



-(void)layoutSubviews{
    
    self.sunDetailLabel.layer.cornerRadius = 15;
    
    self.sunDetailLabel.layer.masksToBounds = YES;
    
    self.monDetailLabel.layer.cornerRadius = 15;
    
    self.monDetailLabel.layer.masksToBounds = YES;
    
    self.tueDetailLabel.layer.cornerRadius = 15;
    
    self.tueDetailLabel.layer.masksToBounds = YES;
    
    self.wedDetailLabel.layer.cornerRadius = 15;
    
    self.wedDetailLabel.layer.masksToBounds = YES;
    
    self.thuDetailLabel.layer.cornerRadius = 15;
    
    self.thuDetailLabel.layer.masksToBounds = YES;
    
    self.friDetailLabel.layer.cornerRadius = 15;
    
    self.friDetailLabel.layer.masksToBounds = YES;
    
    self.satDetailLabel.layer.cornerRadius = 15;
    
    self.satDetailLabel.layer.masksToBounds = YES;
}


/**
 根据时间得到是这个时间是每个月的几号
 @param date 时间
 @return 几号
 */
-(NSInteger)getDayWithDate:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *comp =[calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekOfMonth fromDate:date];

    // 得到几号
    NSInteger day = [comp day];
    
    return day;
}


/**
 得到Date时间段往后或者往前的天数 今天3号  明天就是 3+ 1 四号
 @param date 开始时间
 @param count 距离开始时间的天数
 @return 计算后的天数
 */
-(NSDate *)getDayWithDate:(NSDate *)date andDayCount:(NSInteger)count{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *oldComp =[calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekOfMonth fromDate:date];
    
    NSInteger day = [oldComp day];
    
    [oldComp setDay:day + count];
    
    NSDate *newDate= [calendar dateFromComponents:oldComp];

    return newDate;
    
//    // 得到几号
//    NSInteger newDay = [self getDayWithDate:newDate];
//
//    return newDay;
}


-(BOOL)dateIsToday:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents* comp1 = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    
    NSDateComponents* comp2 = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    
    return  comp1.year == comp2.year && comp1.month == comp2.month && comp1.day == comp2.day;
}
@end
